import java.util.HashMap;

public class TXDigitalIntoChinese {
    /**
     * 思路如下：
     * 1. 使用分割的方法来进行操作；
     * 2. 先处理整数部分
     * 3. 分别除以亿 万
     * 4. 整数部分比较好处理，针对小数部分，首先要取出完整的小数 使用Math.round()函数来进行；
     * @param num
     * @return
     */
    static HashMap<Integer, Character> map;
    static {
        map = new HashMap<Integer, Character>();
        map.put(1, '壹');
        map.put(2, '贰');
        map.put(3, '叁');
        map.put(4, '肆');
        map.put(5, '伍');
        map.put(6, '陆');
        map.put(7, '柒');
        map.put(8, '捌');
        map.put(9, '玖');
    }
    public static String changNum(double num) {
        StringBuilder res = new StringBuilder();
        long integer = (long)num;

        int decimal = (int)Math.round((num - integer) * 1000);
        int hundredMillion = 100000000;
        int tenThousand = 10000;
        long temFirst  = integer / hundredMillion;
        int temOne = (int)temFirst;
        int temTwo = (int)(integer % hundredMillion) / tenThousand;
        long longThree = integer % tenThousand;
        int temThree = (int)longThree;
        if (temOne != 0) {
            res.append(changOne(temOne));
            res.append('亿');
        }
        if (temTwo != 0) {
            res.append(changOne(temTwo));
            res.append('万');
        }
        if (temThree != 0) {
            res.append(changOne(temThree));
        }
        if (res.length() > 0) {
            res.append('圆');
        }
        int firstDecimal = decimal / 100;
        int secondDecimal = (decimal % 100) / 10;
        int threeDecimal = decimal % 10;

        if (firstDecimal != 0) {
            res.append(map.get(firstDecimal));
            res.append('角');
        }
        if (secondDecimal != 0) {
            res.append(map.get(secondDecimal));
            res.append('分');
        }
        if (threeDecimal != 0) {
            res.append(map.get(threeDecimal));
            res.append('厘');
        }

        return res.toString();
    }

    public static String changOne(int value) {

        StringBuilder stringBuilder = new StringBuilder();
        int index = 0;
        int tem =  value / 1000;
        value = value % 1000;
        if (tem > 0) {
            stringBuilder.append(map.get(tem));
            stringBuilder.append('仟');
            index = index + 2;
        }
        tem = value / 100;
        value = value % 100;
        if (tem > 0) {
            stringBuilder.append(map.get(tem));
            stringBuilder.append('佰');
            index = index + 2;
        }
        tem = value / 10;
        value = value % 10;

        if (tem > 0) {
            if (stringBuilder.capacity() > 0 && index > 0 && stringBuilder.charAt(index - 1) == '仟') {
                stringBuilder.append('零');
                stringBuilder.append(map.get(tem));
                stringBuilder.append('拾');
                index = index + 2;
            } else {
                stringBuilder.append(map.get(tem));
                stringBuilder.append('拾');
                index++;
            }

        }
        tem = value;
        if (tem > 0) {
            if (stringBuilder.capacity() > 0 && index > 0 && (stringBuilder.charAt(index - 1) == '仟' || stringBuilder.charAt(index - 1) == '佰')) {
                stringBuilder.append('零');
                stringBuilder.append(map.get(tem));
                index = index + 2;
            } else {
                stringBuilder.append(map.get(tem));
                index++;
            }


        }

        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        // 测试附带结果
        // 壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖圆壹角贰分叁厘
        System.out.println(changNum(123456789.123));
        // 玖拾亿壹仟零贰拾万叁仟零肆拾圆壹角叁厘
        System.out.println(changNum(9010203040.103));

        // 壹亿肆拾伍万陆仟柒佰捌拾玖圆壹角贰分叁厘
        System.out.println(changNum(100456789.123));
        // 玖拾捌亿柒仟陆佰伍拾肆万叁仟贰佰壹拾圆壹角贰分
        System.out.println(changNum(9876543210.120));
        // 玖拾亿圆叁厘
        System.out.println(changNum(9000000000.003));
    }
}
